package cn.myjerry.io.others;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 字节数组 节点流
 * 数组的长度有限 数据量不会很大
 * @author Jerry
 *
 */

public class ByteArrayDemo01 {
	public static void main(String[] args) throws IOException {
		read(write());
	}
	/**
	 * 输出流 操作与文件输出流有些不同 有新增方法 不能使用多态
	 * @return
	 * @throws IOException
	 */
	public static byte[] write() throws IOException {
		byte[] bes;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		String str = "这是测试流01";
		byte[] src = str.getBytes();
		bos.write(src, 0, src.length);
		bes = bos.toByteArray();
		bos.close();
		return bes;
	}
	/**
	 * 输入流 操作与文件输入流一样
	 * 读取字节数组
	 * @param src
	 * @throws IOException
	 */
	public static void read(byte[] src) throws IOException {
//		String str = "这是测试流";
//		byte[] src = str.getBytes();
		
		BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
		byte[] flush = new byte[1024];
		int len = 0;
		while(-1 != (len=is.read(flush))) {
			System.out.println(new String(flush, 0, len));
		}
		is.close();
	}
}
